package com.jack.payment.util.generate;

import com.jack.payment.constant.enums.LoginMethodEnum;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.common.CommonAskHead;
import com.jack.payment.entity.generate.user.UserApiModel;
import com.jack.payment.entity.generate.user.UserAuthModel;
import com.jack.payment.entity.generate.user.UserModel;
import com.jack.payment.entity.generate.user.UserSettingModel;
import com.jack.payment.entity.request.BasicAccountModel;
import com.jack.payment.entity.result.LoginSuccessModel;
import com.jack.payment.token.util.TokenUtil;
import com.jack.payment.util.auth.AuthUtils;
import com.jack.payment.util.common.StringUtil;
import com.jack.payment.util.common.TimeUtil;
import com.jack.payment.util.redis.GenerateId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserGenerate {
    private Logger log = LoggerFactory.getLogger(UserGenerate.class);

    @Autowired
    private GenerateId generateId;
    @Autowired
    private TokenUtil tokenUtil;


    public UserModel updateUserModelThirdAccount(String account,
                                          UserModel userModel,
                                          LoginMethodEnum loginMethodEnum) {
        switch (loginMethodEnum) {
            case TWO_FA ->
                    userModel.setTwoSecret(account);
            case QQ ->
                    userModel.setQqSecret(account);
            case EMAIL ->
                    userModel.setEmail(account);
            case PHONE ->
                    userModel.setPhone(account);
            default ->
                    log.warn("未能找到合适的登录方式");
        }
        return userModel;
    }


    public UserModel updateUserLoginPwd(String userId,
                                        String pwd) {
        UserModel userModel = new UserModel();
        userModel.setId(userId);
        String salt = StringUtil.verifySalt();
        userModel.setSalt(salt);
        userModel.setPwd(AuthUtils.md5Hex(pwd, salt));
        return userModel;
    }




    /**
     * 生成用户设置的一些数
     * @param userId            用户登录
     * @return
     */
    public UserSettingModel generateUserSetting(String userId,
                                                String name
                                                ) {
        UserSettingModel userSettingModel = new UserSettingModel(userId);
        userSettingModel.setName(name);
        userSettingModel.setCreateTime(TimeUtil.getCurrentTime());
        return userSettingModel;
    }


    /**
     * 用户注册时生成 用户基础数据
     * @param loginMethodEnum
     * @return
     */
    public UserModel generateUser(String account,
                                  String pwd,
                                  LoginMethodEnum loginMethodEnum) throws BusinessException {
        UserModel userModel = new UserModel();
        userModel.setId(generateId.generateUserId());
        switch (loginMethodEnum) {
            case TWO_FA -> userModel.setTwoSecret(account);
            case QQ -> userModel.setQqSecret(account);
            case EMAIL -> userModel.setEmail(account);
            case PHONE -> userModel.setPhone(account);
        }
        String pwdSalt = StringUtil.verifySalt();
        userModel.setSalt(pwdSalt);
        userModel.setPwd(AuthUtils.md5Hex(pwd, pwdSalt));
        userModel.setRole("3");
        userModel.setEnable("1");
        return userModel;
    }

    /**
     * 三方登录时, 直接进行注册
     * @param registerModel
     * @return
     */
    public UserAuthModel generateUserAuth(String userId,
                                          BasicAccountModel.AuthLoginRegisterModel registerModel) {
        UserAuthModel userAuthModel = new UserAuthModel();
        userAuthModel.setUser(userId);
        userAuthModel.setChannel(registerModel.getChannel());
        userAuthModel.setSecret(registerModel.getSecret());
        userAuthModel.setThirdAccount(registerModel.getThirdAccount());
        userAuthModel.setEnable(1);
        userAuthModel.setCreateTime(TimeUtil.getCurrentTime());
        return userAuthModel;
    }

    public UserAuthModel generateUserAuth(String userId,
                                          Integer channel,
                                          String secret,
                                          String  thirdAccount) {
        UserAuthModel userAuthModel = new UserAuthModel();
        userAuthModel.setUser(userId);
        userAuthModel.setChannel(channel);
        userAuthModel.setSecret(secret);
        userAuthModel.setThirdAccount(thirdAccount);
        userAuthModel.setEnable(1);
        userAuthModel.setCreateTime(TimeUtil.getCurrentTime());
        return userAuthModel;
    }

    /**
     * 登录成功后返回用户的基础信息
     * @param userModel
     * @param userSettingModel
     * @return
     */
    public LoginSuccessModel generateResultLoginSuccess(CommonAskHead commonAskHead,
                                                        UserModel userModel,
                                                        UserSettingModel userSettingModel) throws BusinessException {
        userSettingModel.setLastOneTime(commonAskHead.getCurrentTime());
        userSettingModel.setIpAddress(commonAskHead.getIpAddress());
        return new LoginSuccessModel().builder()
                .userBasic(userModel)
                .userSetting(userSettingModel)
                .token(tokenUtil.login("admin", userModel.getId(), true))
                .build();
    }

    /**
     * 生成新的api key 信息
     * @param userId
     * @param apiKey
     * @param type
     * @return
     */
    public UserApiModel generateNewUserApi(String userId,
                                           String apiKey,
                                           String name,
                                           Integer type) {
        UserApiModel userApiModel = new UserApiModel();
        userApiModel.setUser(userId);
        userApiModel.setApiKey(apiKey);
        userApiModel.setType(type);
        userApiModel.setEnable(1);
        userApiModel.setPaymentCount(0);
        userApiModel.setCreateTime(TimeUtil.getCurrentTime());
        userApiModel.setName(name);
        return userApiModel;
    }

    /**
     * 用户基础信息修改
     * @param userId
     * @param basicModel
     * @return
     */
    public UserSettingModel updateUserBasicInfo(String userId,
                                                BasicAccountModel.UpdateUserDetailModel basicModel) {
        UserSettingModel userSettingModel = new UserSettingModel();
        userSettingModel.setUserId(userId);
        userSettingModel.setName(basicModel.getName());
        userSettingModel.setMotto(basicModel.getMotto());
        userSettingModel.setIconUrl(basicModel.getIconUrl());
        userSettingModel.setUpdateTime(TimeUtil.getCurrentTime());
        return userSettingModel;
    }
}
